<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>业务数据保存设置</title>
    <#include "/pubHeadStyleTree.html">
    <link rel="stylesheet" href="${ctx}/css/form.css">
    <script src="${ctx}/formStatic/script/InitMirror.js"></script>
    <script src="${ctx}/formStatic/script/codemirror.js"></script>
</head>
<body>
<div style="position: relative;height: 100%;">
    <div class="ccpui-trees">
                <p class="isShowTree colorSwitch">
                    <span style="float: left;color: white">&nbsp;&nbsp;表单字段</span>
                    <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
                </p>
                <div style="margin-top: 6px;">
                    <ul class="ccpui-iconTree iconTrees">
                        <li><a class="iconfont iconshuaxin- zTree-refresh" title="刷新"></a></li>
                        <li><a class="iconfont iconwenjianjia1-copy zTree-triangle-d" title="展开"></a></li>
                        <li><a class="iconfont iconfolder1 zTree-triangle-r" title="收起"></a></li>
                    </ul>
                    <ul id="treeClass" class="ztree ccpui-listZtree" style="background: #fff;overflow: auto;height: 420px;"></ul>
                </div>
    </div>
    <div class="ccpui-coreContent">
        <div class="layui-row" style="margin-top:-11px;">
            <form class="layui-form" action="" lay-filter="userForm" onsubmit="return false;">
                <div class="layui-tab layui-tab-card" style="height:498px">
                    <ul class="layui-tab-title">
                        <li class="layui-this">javaScript前台脚本</li>
                        <li>JAVA后台脚本</li>
                        <li>帮助</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <table class="table-detail">
                                <tr>
                                    <th style="width:16%;">JS前置代码:</th>
                                    <td style="width:100%;padding-left: 0;">
                                        <textarea placeholder="" class="layui-textarea" rows="10" name="jsPreScript" id="jsPreScript"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <th style="width:16%;">JS后置脚本:</th>
                                    <td style="width:100%;padding-left: 0;">
                                        <textarea placeholder="" class="layui-textarea" rows="10" name="jsAfterScript" id="jsAfterScript"></textarea>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="layui-tab-item" style="height: 436px;overflow-y: auto;">
                            <div>
                                <div class="mainFieldChk">
                                    <input type="radio" name="rdoMain" lay-filter="rdoMain" value="1" title="字段" checked="">
                                    <input type="radio" name="rdoMain" lay-filter="rdoMain" value="2" title="代码">
                                </div>
                                <div class="subTableField" style="display:none">
                                    <input type="radio" name="rdoSubField" lay-filter="rdoSubField" value="1" title="字段" checked="">
                                    <input type="radio" name="rdoSubField" lay-filter="rdoSubField" value="2" title="遍历字段">
                                </div>
                            </div>
                            <table class="table-detail">
                                <tr>
                                    <th style="width:16%;">JAVA前置代码:</th>
                                    <td style="width:100%;padding-left: 0;">
                                        <div class="CodeMirror-wrapping" style="background-color: white;height: 190px;">
                                            <textarea class="layui-textarea" codemirror="true" mirrorheight="190px" rows="10" name="preScript" id="preScript"></textarea>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th style="width:16%;">JAVA后置代码:</th>
                                    <td style="width:100%;padding-left: 0;">
                                        <div class="CodeMirror-wrapping" style="background-color: white;height: 190px;">
                                            <textarea class="layui-textarea" codemirror="true" mirrorheight="190px" rows="10" name="afterScript" id="afterScript"></textarea>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="layui-tab-item">
                            <table class="table-detail">
                                <tr>
                                    <th style="width:16%;">Javascript代码:</th>
                                    <td style="width:100%;background-color: white;">
                                        <pre>前置Javascript需要返回boolean值。 后置脚本在数据提交后进行处理。</pre>
                                    </td>
                                </tr>
                                <tr>
                                    <th style="width:16%;">JAVA代码:</th>
                                    <td style="width:100%;background-color: white;">
                                        <pre>
在java代码中可以使用data对象，data为FormData的对象实例。
FormData结构如下：

import com.ccp.dev.form.model.SubTable;
import com.ccp.dev.form.model.FormData;
import java.util.*;

SubTable subTable= data.getSubTableByName(String tableName);
// 子表遍历
List&lt;Map&lt;String, Object&gt;&gt; rows= subTable.getDataList();
for(Map&lt;String, Object&gt; row:rows){
    // 设置值
    row.put("key",123);
}

// 判断数据是否存在
scriptImpl.validDataExist(data,"字段名称");
                                        </pre>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="layui-form-item" style="position: absolute;bottom: -2px;left: 42%;">
                        <div>
                            <input type="hidden" name="id" id="sysBusEventId"/>
                            <input type="hidden" id="formKey" name="formKey" value="${formKey}"/>
                        </div>
                        <div style="">
                            <button lay-submit lay-filter="formBusinessKeep" class="layui-btn btn-n">保存</button>
                            <button class="layui-btn layui-btn-danger" id="return">取消</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var formKey = '${formKey}';
    layui.config({
        base: serverPath.formPath + staticPath.formPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index', 'table', 'layer', 'jquery', 'form', 'tree','commons', 'totalFilter', 'setter'], function () {
        var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons,
            totalFilter = layui.totalFilter, form = layui.form;

        var index = parent.layer.getFrameIndex(window.name);
        $('#return').click(function () {
            parent.layer.close(index);
        });
        //刷新
        $(".zTree-refresh").click(function () {
            resZtrees();
        });
        //展开
        $(".zTree-triangle-d").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(true);
        });
        //收起
        $(".zTree-triangle-r").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(false);
        });
        resZtrees();
        totalFilter.init();
        //获取ztree结构数据
        function resZtrees() {
            $.ajax({
                type: "post",
                dataType: 'json',
                data: {"formKey": formKey},
                url: serverPath.formPath + setter.formLinkPath +"table/getJsonByFormKey",
                success: function (data) {
                    var setting = {
                        view: {
                            showLine: false,
                            nameIsHTML: true, //允许name支持html
                            selectedMulti: false
                        },
                        edit: {
                            enable: false,
                            editNameSelectAll: false
                        },
                        data: {
                            key: {
                                name: "comment",
                                title:"name"
                            }
                        },

                        callback: {
                            onClick: onClickType,
                            onDblClick:zTreeOnDblClick
                        }
                    };
                    $.fn.zTree.init($('#treeClass'), setting, data);
                }
            });
        }
        loadAjax();
        //加载数据
        function loadAjax(){
             $.ajax({
                 type: "post",
                 dataType: 'json',
                 data: {"formKey": formKey},
                 url: serverPath.formPath + setter.formLinkPath +"sysBusEvent/edit",
                 success: function (data) {
                     var returnDate = data.data;
                     if(returnDate!=null) {
                         $("#jsPreScript").val(returnDate.jsPreScript);
                         $("#jsAfterScript").val(returnDate.jsAfterScript);
                         $("#formKey").val(returnDate.formKey);
                         $("#sysBusEventId").val(returnDate.id);
                         InitMirror.getById("preScript").insertCode(returnDate.preScript);
                         InitMirror.getById("afterScript").insertCode(returnDate.afterScript);
                     }
                 }
             });
        }

        function onClickType(event, treeId, treeNode) {
            var isTable=treeNode.isTable;
            var isMain=treeNode.isMain;
            if(isMain === 0 && isTable === 0){
                $(".subTableField").show();
                $(".mainFieldChk").hide();
            }else if(isMain === 1 && isTable === 0) {
                $(".mainFieldChk").show();
                $(".subTableField").hide();
            }
        }
        //树状菜单隐藏
        var treeval = true;
        $(".treeone").click(function () {
            treeval = tresStyle(treeval, ".treeone");
        });
        function onClickRess(event, treeId, treeNode, clickFlag) {
            selectOrgId = treeNode.orgId;
        }
        //保存业务逻辑
        form.on('submit(formBusinessKeep)', function (data) {
            var preScript=InitMirror.getById("preScript").getCode();
            var afterScript=InitMirror.getById("afterScript").getCode();
            data.field.preScript=preScript;
            data.field.afterScript=afterScript;
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath +"sysBusEvent/save",
                type: "POST",
                contentType: 'application/json',
                dataType: 'json',
                data:JSON.stringify(data.field),
                success: function (result) {
                    if (commons._filterRecord(result)) {
                        layer.alert(result.msg,{skin: 'btn-class'}, function () {

                            parent.layui.table.reload('customForm', {
                                page: {
                                    curr: 1
                                }
                            });
                            parent.layui.totalFilter.init('customForm');
                            commons._bulletWindow(index);
                        });
                    } else {
                        window.parent.layer.alert(result.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                }
            });
            return false;
        });
        //点击字段，在光标处自动插入代码
        function zTreeOnDblClick(event, treeId, node){
            var name=node.name;
            var isTable=node.isTable;
            var isExternal=node.isExternal;
            var isMain=node.isMain;
            var code="";
                //data.getMainField
                //为表
                if(isTable === 1){
                    if(isMain === 1){
                        code=(isExternal === 1)?name:"W_" +name;
                    }
                    else{
                        code="SubTable subTable= data.getSubTableByName(\""+name+"\");";
                        name=(isExternal === 1)?name:"W_"+name;
                    }
                }
                //字段
                else{
                    //主表字段
                    if(isMain === 1){
                        var checkedMainRadio = $('input[name="rdoMain"]:checked').val();
                        if(checkedMainRadio === 2){
                            code="String "+name+"=data.getMainField(\""+name+"\");";
                        }
                        else{
                            code=name;
                        }
                    }
                    else{
                        var checkedSubRadio=$('input[name="rdoSubField"]:checked').val();
                        if(checkedSubRadio === 2){
                            code="List<Map<String, Object>> rows = subTable.getDataList();\n"+
                                " for(Map<String, Object> row : rows){\r\n" +
                                " Object obj = row.get(\""+name+"\");\r\n" +
                                "}"
                        }
                        else{
                            code=name;
                        }
                    }
            }
            InitMirror.editor.insertCode(code);
        }
    });
</script>
</body>
</html>